﻿using System;
using GDIT.Base.PageBaseHelper;
using GDIT.BLL;
using GDIT.Model;

namespace GDIT.page.manage.notice
{
    public partial class editor : System.Web.UI.Page
    {
        public string url = string.Empty;
        private BBulletinArticle bulletinEditor = null;
        private Mtb_BulletinArticle bulletin = null;
        private Mtb_CollegeInformationArticle article = null;
        private BCollegeInformationArticle collegeEditor = null;
        private Mtb_Users user = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
                this.txtId.Text = Request.QueryString["id"];
                this.txtType.Text = Request.QueryString["type"];
                url = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Admin/fckeditor/asp.net/upload.aspx";
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        

        protected void btnPublish_Click(object sender, EventArgs e)
        {
             if (ddlType.SelectedValue == "公告")
            {
                //发布公告
                OperationBulletin();
            }
            else if (ddlType.SelectedValue == "资讯")
            {
                //发布资讯
                OperationCollege();
            }
            //部门根据用户
        }

        /// <summary>
        /// 保存通知，修改通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtType.Text == "bulletin")//修改公告
            {
                OperationBulletin();
            }
            else if (txtType.Text == "information")//修改资讯
            {
                OperationCollege();
            }
        }

        private void OperationBulletin()
        {
            bulletin = new Mtb_BulletinArticle();
            try
            {
                bulletin.Title = this.txtTitle.Text.Trim();
                bulletin.Contents = this.content1.Value;
                string[] arr = this.txtKey.Text.Trim().Split(',');
                for (int i = 0; i <= arr.Length; i++)
                {
                    Mtb_BullentinKeyWord bulletinKey = new Mtb_BullentinKeyWord();
                    bulletinKey.AritlceKeyWord = arr[i];
                    bulletin.KeyWords.Add(bulletinKey);
                }
                if (this.chkImportant.Checked)
                    bulletin.Important = "是";
                else
                    bulletin.Important = "否";
                if (this.chkTop.Checked)
                    bulletin.Top = "是";
                else
                    bulletin.Top = "否";
                if (this.chkInner.Checked)
                    bulletin.Internal = "是";
                else
                    bulletin.Internal = "否";
                bulletinEditor = new BBulletinArticle();
                bool result = false;
                if (string.IsNullOrEmpty(txtId.Text.Trim()) == false)
                {
                    //如果检查到有ID的话，就说明是直接修改
                    bulletin.OID = Convert.ToInt32(txtId.Text.Trim());
                    result = bulletinEditor.UpdateBulletinArticle(bulletin);
                }
                else
                {
                    user = new Mtb_Users();
                    user.UserName = Session["UserName"].ToString();
                    user.BulletinArticles.Add(bulletin);
                    result = bulletinEditor.InsertBulletinArticle(user);
                }
                if (!result)
                    PageBase.MessageBoxShow("抱歉，操作公告失败！", Page);
                else
                    PageBase.MessageBoxShow("恭喜，操作公告成功！", Page);
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }

        private void OperationCollege()
        {
            article = new Mtb_CollegeInformationArticle();
            try
            {
                article.Title = this.txtTitle.Text.Trim();
                article.Contents = this.content1.Value;
                string[] arr = this.txtKey.Text.Trim().Split(',');
                for (int i = 0; i <= arr.Length-1; i++)
                {
                    Mtb_CollegeKeyWord articleKey = new Mtb_CollegeKeyWord();
                    articleKey.AritlceKeyWord = arr[i];
                    article.KeyWords.Add(articleKey);
                }
                if (this.chkImportant.Checked)
                    article.Important = "是";
                else
                    article.Important = "否";
                if (this.chkTop.Checked)
                    article.Top = "是";
                else
                    article.Top = "否";
                if (this.chkInner.Checked)
                    article.Internal = "是";
                else
                    article.Internal = "否";
                bool result = false;
                if (string.IsNullOrEmpty(txtId.Text.Trim()) == false)
                {
                    collegeEditor = new BCollegeInformationArticle();
                    //如果检查到有ID的话，就说明是直接修改
                    article.OID = Convert.ToInt32(txtId.Text.Trim());
                    result = collegeEditor.UpdateCollegeInformationArticle(article);
                }
                else
                {
                    collegeEditor = new BCollegeInformationArticle();
                    user = new Mtb_Users();
                    user.UserName = Session["UserName"].ToString();
                    user.CollegeInformationArticles.Add(collegeEditor);
                    result = collegeEditor.InsertCollegeInformationArticle(user);
                }
                if (!result)
                    PageBase.MessageBoxShow("抱歉，操作校园资讯失败！", Page);
                else
                    PageBase.MessageBoxShow("恭喜，操作校园资讯成功！", Page);
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
    }
}